package com.offcn.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.Query;
import com.offcn.member.dao.MemberDao;
import com.offcn.member.entity.MemberEntity;
import com.offcn.member.service.MemberService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> countByDateTime(String beginTime, String endTime) {
       //创建查询条件封装对象
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        //设置查询条件 slect
        queryWrapper.select("DATE_FORMAT(create_time,'%Y-%m-%d') AS NAME,COUNT(id) AS VALUE");
        //设置group by
        queryWrapper.groupBy("NAME");
        //设置按照日期时间范围进行过滤
        queryWrapper.between("create_time",beginTime+" 00:00:00",endTime+" 23:59:59");


        return this.baseMapper.selectMaps(queryWrapper);
    }

    @Override
    public MemberEntity login(String username, String password) {
        //创建查询条件封装对象
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        //设置查询条件1：用户名
        queryWrapper.eq("user_name",username);
        //设置查询条件2：密码
        queryWrapper.eq("password",password);
        return this.getOne(queryWrapper);
    }
}